import React from 'react';
import accounting from 'accounting';

const accountingOptions = {
  symbol: "¥",
}
accounting.setting = accountingOptions

const formatNumber = (value = 0, d = 2) => {
  return value.toFixed(d);
};

const formatMoney = (value = 0, d = 2) => {
  // return '￥'+ value.toFixed(d);
  return accounting.formatMoney(value, accountingOptions);
};

const colorMoney = (text) => {
  const n = Number.parseFloat(text);
  const t = formatMoney(text);
  const color = n < 0 ? 'green' : 'red';
  return (<span style={{ color }}>{t}</span>);
};

const formatSeconds = (s) => {
  let days = parseInt(s / (60 * 60 *24));
  let hours = parseInt(s % (60 * 60 * 24) / (60 * 60));
  let minutes = parseInt((s % (60 * 60)) / 60);
  let seconds = parseInt(s % 60);
  if (days >=1 ){
    return `${days}天 ${hours}小时 ${minutes}分 ${seconds}秒`;
  } else {
    return `${hours}小时 ${minutes}分 ${seconds}秒`;
  }  
};

const formatRoi = (roi) => {
  return (roi * 100).toFixed(2) + '%';
}

export default {
  formatNumber,
  formatMoney,
  colorMoney,
  formatSeconds,
  formatRoi,
  accounting,
}
